class Solution {
    public int maxResult(int[] nums, int k) {
        int n = nums.length;
        int[] res = new int[n];
        res[0] = nums[0];
        // int tmp;
        final PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)->b[1]-a[1]);
        pq.add(new int[]{0, res[0]});
        for (int i = 1; i < n; i++) {
            // tmp = Integer.MIN_VALUE;
            // for (int j = 1; j <= k; j++) {
            //     if (i - j >= 0 && i - j < n) {
            //         tmp = Math.max(tmp, res[i - j]);
            //     }
            // }
            while (i-pq.peek()[0] > k) {
                pq.poll();
            }
            res[i] = nums[i] + pq.peek()[1];
            pq.add(new int[]{i, res[i]});
        }
        return res[n - 1];
    }
}